﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

namespace SAC.Helper
{
    public class LogHelper
    {
        /// <summary>
        /// 日志写入类
        /// </summary>
        public LogHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 日志类别
        /// </summary>
        public enum EnLogType
        {
            /// <summary>
            /// 错误日志
            /// </summary>
            Error,
            /// <summary>
            /// 成功日志
            /// </summary>
            Success,
            /// <summary>
            /// 日常日志
            /// </summary>
            Common,
            /// <summary>
            /// 警告日志
            /// </summary>
            Warn,
            /// <summary>
            /// 运行日志
            /// </summary>
            Run
        }

        /// <summary>
        /// 记录错误日志至文本文件
        /// </summary>
        /// <param name="message">记录的内容</param>
        public static void WriteLog(string message)
        {
            WriteLog(EnLogType.Error, message);
        }

        private static object privateObjectLock = new object();

        /// <summary>
        /// 记录日志至文本文件
        /// </summary>
        /// <param name="logtype">日志类型</param>
        /// <param name="message">记录的内容</param>
        public static void WriteLog(EnLogType logtype, string message)
        {
            string filePrefix;
            switch (logtype)
            {
                case EnLogType.Common:
                    filePrefix = "Access_";
                    break;
                case EnLogType.Error:
                    filePrefix = "Error_";
                    break;
                case EnLogType.Success:
                    filePrefix = "Sucess_";
                    break;
                case EnLogType.Warn:
                    filePrefix = "Warn_";
                    break;
                case EnLogType.Run:
                    filePrefix = "Run_";
                    break;
                default:
                    filePrefix = "Error_";
                    break;
            }
            string path;
            try
            {
                //path = System.Web.HttpContext.Current.Server.MapPath("~/") + ConfigurationSettings.AppSettings["LogPath"] + "\\";
                path = "e:\\Log\\";
            }
            catch (Exception e)
            {
                return;
            }

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            lock (privateObjectLock)
            {
                try
                {
                    string FILE_NAME = path + filePrefix + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                    if (File.Exists(FILE_NAME))
                    {
                        StreamWriter sr = File.AppendText(FILE_NAME);
                        sr.WriteLine(DateTime.Now.ToString() + ": " + message);
                        sr.Close();
                    }
                    else
                    {
                        StreamWriter sr = File.CreateText(FILE_NAME);
                        sr.WriteLine(DateTime.Now.ToString() + ": " + message);
                        sr.Close();
                    }
                }
                catch { }
            }
        }





        public static void ToCsv(string[] zb, string[] un, string[] lay, string[] dv)
        {
            //csv += sb.ToString();
            ////下面将文本输出为csv个数，可用excel直接打开
            //Response.Clear();
            //Response.Buffer = true;
            //Response.Charset = "gb2312";
            ////下面这行很重要， attachment 参数表示作为附件下载，您可以改成 online在线打开
            ////filename=FileFlow.xls 指定输出文件的名称，注意其扩展名和指定文件类型相符，可以为：.doc .xls .txt 　.htm　　
            //Response.AppendHeader("Content-Disposition", "attachment;filename=\"OrgCount.csv\"");
            //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            ////Response.ContentType指定文件类型 可以为application/ms-excel 　 application/ms-word 　 application/ms-txt　 application/ms-html 　或其他浏览器可直接支持文档　
            //Response.ContentType = "application/ms-excel";
            //this.EnableViewState = false;
            //System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            //oStringWriter.Write(csv);
            //oStringWriter.Flush();
            //System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            //this.RenderControl(oHtmlTextWriter);
            ////this 表示输出本页，你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件　
            //Response.Write(oStringWriter.ToString());
            //Response.End();
        }
    }
}
